﻿$.fn.dropdown = function(options) {
    // Build main options before element iteration
    var settings = $.extend({}, $.fn.dropdown.defaults, options);

    return this.each(function() {
      $.dropdown(this, settings);
    });
  };
  
  // Plugin defaults.
  $.fn.dropdown.defaults = {
    texts:[],
	values:[],
	topx:0,
	leftx:0,
	heightx:0,
	setWidth:true, //2012-02-23
	widthx:0 // 调节宽度的 2012-02-22 by liusan.dyf
  };

  $.dropdown = function (elm, settings) {
	var $e = $(elm);
	$e.attr('autocomplete', 'OFF'); 
	
	var selectedClass = "selected";
    var selectedSelector = "li." + selectedClass;
	
	// 下拉
	var $ddDiv = $('<div class="dropdown"></div>');
    var $ddList = $('<ul></ul>');
	
	// Build the list.
	if(!settings.values)
		settings.values = [];
	var valueL = settings.values.length;
	var valueTemp;
    for(var i = 0; i < settings.texts.length; i++) {
		if(valueL>i)
			valueTemp = settings.values[i];
		else
			value=settings.texts[i];
			
		$ddList.append("<li data-value='"+valueTemp+"'>" + settings.texts[i] + "</li>");
    }
	
    $ddDiv.append($ddList);
    $ddDiv.appendTo('body').hide();
	
	// 浮动的事情，交给floatX插件 2012-02-22
	settings.floatingObject = $ddDiv;
	$.floatX(elm,settings);
		
	// 取消全部事件后再绑定	
	$("li", $ddDiv).unbind().mouseover(function() {
        $(selectedSelector, $ddDiv).removeClass(selectedClass);
        $(this).addClass(selectedClass);
    }).click(function() {
		var $this = $(this);
      $e.val($this.text());
	  // alert($this.attr("data-value"));
	  $e.attr("data-value",$this.attr("data-value"));
	  $ddDiv.hide();// 点了就隐藏
    });	
	
  };
  
  //----------------------------floatX插件 2012-02-22 by liusan.dyf
  
  $.fn.floatX = function(options) {
    // Build main options before element iteration
    var settings = $.extend({}, $.fn.floatX.defaults, options);

    return this.each(function() {
      $.floatX(this, settings);
    });
  };
  
  // Plugin defaults.
  $.fn.floatX.defaults = {
	floatingObject:null,
	topx:0,
	leftx:0,
	heightx:0,
	widthx:0,
	beforeShow:function($e,$f){},
	afterShow:function($e,$f){},
	setWidth:true //2012-02-23
  };

  $.floatX = function (elm, settings) {
	var $e = $(elm);
	
	var isOver = false;
	var $f;
	
	if(settings.floatingObject!=null)
		$f = $(settings.floatingObject);
	else{
		var floatId = $e.attr("data-floatId");
		$f = $("#"+floatId);
	}
	
	// 取消全部事件后再绑定	
	// 2012-02-23取消unbind
	$f./*unbind().*/mouseenter(function() {
        isOver = true;
    }).mousedown(function() {
       isOver = true;
    }).mouseleave(function() {//该事件很有用
		$f.hide();
    }).mouseout(function() {
       isOver = false;   
    });
	
	$e.click(show).focus(show);
	
	// Hide on blur，这同时导致了一个问题：同一个对象被多个对象使用时会引起混乱，多个对象的blur事件
	// 2012-02-23取消unbind解决上述问题
    $e.blur(function() {
      if (!isOver) {
        $f.hide();
      }
    });
	
	// 显示
	function show(){
		if(settings.beforeShow)
			settings.beforeShow($e,$f);
		// Position
		var elmOffset = $e.offset();
		$f.css({'top':elmOffset.top + elm.offsetHeight + settings.topx, 'left':elmOffset.left + settings.leftx});
		
		if(settings.setWidth) //2012-02-23
			$f.css({'width':$e.width() + settings.widthx});
		  
		$f.show();  
		
		if(settings.afterShow)
			settings.afterShow($e,$f);
	}
  };